var baseurl='http://api.pyful.com';
var imgurl='http://img.9lele.com';
// 配置的小程序APPID和APPsecret
// let app={
//   AppID:'wx78543c41b3c5bcf5',
//   AppSecret:'3cfffcacabcc56e743c85c79fb85ffc7'
// }

// 获取首页新品
const shouGoods=async function(){
  return new Promise((resolve,rej)=>{
    wx.request({
      url: baseurl+'/goods/is_new/1', //接口地址
      data:{
        page:1
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success (res) {
        // console.log(res.data)
        resolve (res.data)
        
      }
    })
  })
}

// 获取为你推荐
const tuiGoods=async function(num){
  return new Promise((resolve,rej)=>{
    wx.request({
      url: baseurl+'/goods/is_recommend/'+num, //接口地址
      header: {
        'content-type': 'application/json' // 默认值
      },
      success (res) {
        // console.log(res.data)
        resolve (res.data)
        
      }
    })
  })
}
// 商品详情
const productDetail=async function(id){
  return new Promise((resolve,rej)=>{
    wx.request({
      url: baseurl+'/goods/detail/'+id, //接口地址
      header: {
        'content-type': 'application/json' // 默认值
      },
      success (res) {
        console.log(res.data)
        resolve (res.data)
        
      }
    })
  })
}
// 首页轮播图
const shoubo=async function(){
  return new Promise((resolve,rej)=>{
    wx.request({
      url: baseurl+'/bannerList', //接口地址
      header: {
        'content-type': 'application/json' // 默认值
      },
      success (res) {
        // console.log(res.data)
        resolve (res.data)
        
      }
    })
  })
}
// 首页分类请求相关数据
const shoufenlei=async function(pages,ids){
  return new Promise((resolve,rej)=>{
    wx.request({
      url: baseurl+'/goods/getMenuGoodsList/'+pages+'/'+ids, 
      header: {
        'content-type': 'application/json' // 默认值
      },
      success (res) {
        resolve(res.data)
      }
    })
  })
}

// 更新用户信息头像
const updateUserinfo=async function(memberid,Name,Img){
  return new Promise((resolve,rej)=>{
    wx.request({
      url: baseurl+'/saveUser',
      data:{
        member_id:memberid,
        nickName:	Name,
        headerImg:Img
      },
      // method:'POST',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success (res) {
        // console.log(res.data)
        resolve(res.data)
      }
    })
  })
}

// 加入购物车
const addcart =async function(obj){
  console.log(116,obj)
  return new Promise((resolve,rej)=>{
    wx.request({
      url: baseurl+'/addcart',
      method:'POST',
      data:{
        goods_id:obj.goods_id,
        goods_name:obj.goods_name,
        price:obj.price,
        number:obj.number,
        sku_id:obj.sku_id,
        sku_pname:obj.sku_pname,
        sku_sname:obj.sku_sname,
        goods_img:obj.goods_img,
        remark:obj.remark,
        member_id:obj.member_id,
        temp_key:"123456789"
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success (res) {
        resolve(res.data)
      }
    })
  })
}

// 获取购物车列表
const getShopCarList=async function(memberid){
  return new Promise((resolve,rej)=>{
    wx.request({
      url: baseurl+'/getCartList', //仅为示例，并非真实的接口地址
      data: {
        member_id:memberid,
        temp_key:"123456789"
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success (res) {
        resolve(res.data)
      }
    })
  })
}
// 删除购物车某个商品
const deleteone=async function(ids){
  return new Promise((resolve,rej)=>{
    wx.request({
      url: baseurl+'/deletecart?id='+ids, //仅为示例，并非真实的接口地址
      header: {
        'content-type': 'application/json' // 默认值
      },
      success (res) {
        resolve(res.data)
      }
    })
  })
}
// 清空购物车
const deleteAll=async function(memberid){
  return new Promise((resolve,rej)=>{
    wx.request({
      url: baseurl+'/clearcart', //仅为示例，并非真实的接口地址
      data:{
        member_id:memberid
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success (res) {
        resolve(res.data)
      }
    })
  })
}

// 搜索商品
const searchGood=async function(page,word){
  return new Promise((resolve,rej)=>{
    wx.request({
      url: baseurl+'goods/search/:'+page+'/:'+word, //仅为示例，并非真实的接口地址
      // data:{
      //   keyword:word,
      //   page:page
      // },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success (res) {
        resolve(res.data)
      }
    })
  })
}


module.exports = {
  shouGoods,tuiGoods,productDetail,shoubo,shoufenlei,baseurl,imgurl,updateUserinfo,addcart,getShopCarList,deleteone,deleteAll,searchGood
}
